<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>todo</title>
    <!-- Bootstrap -->
    <link
            href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
            rel="stylesheet"
    />
    <style>
        .m {
            margin-top: 20px;
        }
        .inputStyle {
            width: 400px;
        }
    </style>
</head>
<body>
<div id="app">
    <div class="row">
        <div class="col-md-3"></div>
        <div class="col-md-6 m">
            <div class="input-group inputStyle">
                <input type="text" class="form-control" v-model.trim="searchData" />
                <span class="input-group-btn">
                            <button class="btn btn-primary" type="button">搜索</button>
                        </span>
            </div>
            <table class="table table-striped table-hover m">
                <thead>
                <tr>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>年龄</th>
                    <th>工号</th>
                    <th>岗位</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="(item,index) in filterEmpList" :key="index">
                    <!-- v-if="item.name.indexOf(searchData)>=0 || item.post.indexOf(searchData)>=0" -->
                    <td>{{item.name}}</td>
                    <td>{{item.sex | formatterSex}}</td>
                    <td :style="{ color: item.age > 18 ? 'red': ''}">{{item.age}}</td>
                    <td>{{item.empNo}}</td>
                    <td>{{item.post}}</td>
                    <td>
                        <!-- 调用opt组件 -->
                        <opt
                                :rowdata="item"
                                :idx="index"
                                :list="filterEmpList"
                                :updatedlist.sync="empList"
                                :eidtdata.sync="form"
                                :type.sync="type"
                        ></opt>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
        <div class="col-md-3"></div>
    </div>

    <!-- 模态框（Modal） -->
    <div
            class="modal fade"
            id="myModal"
            tabindex="-1"
            role="dialog"
            aria-labelledby="myModalLabel"
            aria-hidden="true"
    >
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button
                            type="button"
                            class="close"
                            data-dismiss="modal"
                            aria-hidden="true"
                    >
                        &times;
                    </button>
                    <h4 class="modal-title" id="myModalLabel">新增员工信息</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div :class="['form-group',nameTip]">
                            <label for="firstname" class="col-sm-2 control-label"
                            >姓名</label
                            >
                            <div class="col-sm-10">
                                <input
                                        type="text"
                                        class="form-control inputStyle"
                                        id="firstname"
                                        v-model="form.name"
                                        placeholder="请输入姓名"
                                />
                            </div>
                        </div>
                        <div :class="['form-group',sexTip]">
                            <label for="lastname" class="col-sm-2 control-label"
                            >性别</label
                            >
                            <div class="col-sm-10">
                                <label class="radio-inline">
                                    <input
                                            type="radio"
                                            name="optionsRadiosinline"
                                            id="optionsRadios3"
                                            value="1"
                                            v-model="form.sex"
                                            checked
                                    />
                                    男
                                </label>
                                <label class="radio-inline">
                                    <input
                                            type="radio"
                                            name="optionsRadiosinline"
                                            id="optionsRadios4"
                                            value="2"
                                            v-model="form.sex"
                                    />
                                    女
                                </label>
                            </div>
                        </div>
                        <div :class="['form-group',ageTip]">
                            <label for="lastname" class="col-sm-2 control-label"
                            >年龄</label
                            >
                            <div class="col-sm-10">
                                <input
                                        type="number"
                                        class="form-control inputStyle"
                                        id="lastname"
                                        placeholder="请输入年龄"
                                        v-model="form.age"
                                />
                            </div>
                        </div>
                        <div :class="['form-group',empNoTip]">
                            <label for="lastname" class="col-sm-2 control-label"
                            >工号</label
                            >
                            <div class="col-sm-10">
                                <input
                                        type="number"
                                        class="form-control inputStyle"
                                        id="lastname"
                                        placeholder="请输入工号"
                                        v-model="form.empNo"
                                />
                            </div>
                        </div>
                        <div :class="['form-group',postTip]">
                            <label for="lastname" class="col-sm-2 control-label"
                            >岗位</label
                            >
                            <div class="col-sm-10">
                                <input
                                        type="text"
                                        class="form-control inputStyle"
                                        id="lastname"
                                        placeholder="请输入岗位"
                                        v-model="form.post"
                                />
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">
                        关闭
                    </button>
                    <button
                            type="button"
                            class="btn btn-primary"
                            @click="handleSubmit"
                            ref="submit"
                    >
                        确定
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- opt组件 -->
<template id="option">
    <div>
        <button
                class="btn btn-primary btn-sm"
                data-toggle="modal"
                data-target="#myModal"
                @click="openDialog('add')"
        >
            新增
        </button>
        <button
                class="btn btn-primary btn-sm"
                data-toggle="modal"
                data-target="#myModal"
                @click="openDialog('edit')"
        >
            编辑
        </button>
        <button class="btn btn-primary btn-sm" @click="deleteRow">删除</button>
    </div>
</template>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
<script src="../vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                // 提示
                nameTip: '', // has-error has-success
                sexTip: '', // has-error has-success
                ageTip: '', // has-error has-success
                empNoTip: '', // has-error has-success
                postTip: '', // has-error has-success
                searchData: '',
                type: '',
                // 基础数据
                empList: [
                    {
                        name: '张儒阳',
                        sex: '1',
                        age: '18',
                        empNo: '10001',
                        post: '高级前端开发工程师',
                    },
                    {
                        name: '郑维山',
                        sex: '2',
                        age: '19',
                        empNo: '10002',
                        post: '中级前端开发工程师',
                    },
                ],
                // 表单绑定
                form: {
                    name: '',
                    sex: '1',
                    age: '',
                    empNo: '',
                    post: '',
                },
            };
        },
        filters: {
            // 格式化年龄
            formatterSex(val) {
                if (val === '1') {
                    return '男';
                } else if (val === '2') {
                    return '女';
                }
            },
        },
        computed: {
            // table数据
            filterEmpList() {
                return this.empList.filter((item) => {
                    return (
                        // 名字
                        item.name.indexOf(this.searchData) != -1 ||
                        // 性别
                        item.sex.indexOf(
                            this.searchData === '男'
                                ? '1'
                                : this.searchData === '女'
                                ? '2'
                                : this.searchData,
                        ) != -1 ||
                        // 年龄
                        item.age.indexOf(this.searchData) != -1 ||
                        // 员工号
                        item.empNo.indexOf(this.searchData) != -1 ||
                        // 岗位
                        item.post.indexOf(this.searchData) != -1
                    );
                });
            },
        },
        methods: {
            // 提交
            handleSubmit() {
                // 添加提示的class
                for (const key in this.form) {
                    /*
                        key:name,post,sex...
                        this.form[key]:就是表单的值
                        !this.form[key]:如果这个值为空则怎么样
                        key + 'Tip':nameTip...
                        has-error:bootstrap的默认样式
                    */
                    if (!this.form[key]) {
                        //表单某个字段没有值的情况
                        this[key + 'Tip'] = 'has-error';
                        return false;//阻止代码往下执行(这里是阻止提交)
                    } else {
                        //表单有值的情况
                        this[key + 'Tip'] = '';
                    }
                }
                if (this.type === 'add') {
                    // 拷贝一份form数据,
                    this.copyForm = JSON.parse(JSON.stringify(this.form));
                    this.empList.push(this.copyForm);
                    this.$refs.submit.dataset.dismiss = 'modal';//隐藏模态框
                    // 再把表单的数据清除掉
                    this.clearForm();
                } else if (this.type === 'edit') {
                    for (let index = 0; index < this.empList.length; index++) {
                        if (this.empList[index].empNo === this.form.empNo) {
                            // 拷贝一份form数据,
                            this.copyForm = JSON.parse(JSON.stringify(this.form));
                            this.empList.splice(index, 1, this.copyForm);
                        }
                    }
                    this.$refs.submit.dataset.dismiss = 'modal';//隐藏模态框
                    // 再把表单的数据清除掉
                    this.clearForm();
                }
            },
            // 清除表单的方法
            clearForm() {
                for (const key in this.form) {
                    if (this.form.hasOwnProperty(key)) {
                        console.log(this.form.hasOwnProperty(key))
                        this.form[key] = '';
                    }
                }
            },
        },
        components: {
            // 新增和编辑按钮组件
            opt: {
                props: {
                    // 每行的数据
                    rowdata: {
                        type: Object,
                    },
                    // 索引
                    idx: {
                        type: Number,
                    },
                    // table数据
                    list: {
                        type: Array,
                    },
                },
                template: '#option',
                data() {
                    return {};
                },
                methods: {
                    // 打开模态框
                    openDialog(type) {
                        this.$emit('update:type', type);
                        if (type === 'add') {
                            // 打开模态框,重置表单数据
                            let form = {
                                name: '',
                                sex: '1',
                                age: '',
                                empNo: '',
                                post: '',
                            };
                            this.$emit('update:eidtdata', form);
                        } else if (type === 'edit') {
                            this.$emit('update:eidtdata', this.rowdata);
                        }
                    },
                    // 删除行
                    deleteRow() {
                        this.list.splice(this.idx, 1);
                        this.$emit('update:updatedlist', this.list);
                    },
                },
            },
        },
    });
</script>
</body>
</html>
